/**
 * 
 */
package TestCases;

import java.util.Vector;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import Tracer.*;
import junit.framework.TestCase;
import XMLScene.*;

/**
 * @author Hongyi Zhao
 * 
 */
public class RTImportXMLTest extends TestCase {
	private RTImportXML importXML = null;

	/**
	 * @param name
	 */
	public RTImportXMLTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();

		// Initialize xml reader.
		XMLReader parser = XMLReaderFactory.createXMLReader();
		importXML = new RTImportXML();
		parser.setFeature("http://xml.org/sax/features/validation", true);
		parser.setContentHandler(importXML);

		parser.parse("For JUnit Test.xml");

	}

	protected void tearDown() throws Exception {
		super.tearDown();
		importXML = null;
	}

	/**
	 * Test method for startDocument Test it works well after performing
	 * startDocument
	 */
	public final void testStartDocument() {
		assertNotNull("Object list initialized failed", importXML
				.getObjectList());
		assertNotNull("Light list initialized failed", importXML.getLightList());
	}

	/**
	 * Test method for endDocument Test it works well after performing
	 * endDocument
	 */
	public final void testEndDocument() {
		RTColor background = importXML.getBackground();
		Vector<RTIObject> objectList = importXML.getObjectList();
		Vector<RTLight> lightList = importXML.getLightList();
		RTCamera camera = importXML.getCamera();

		assertNotNull("Background is null", background);
		assertNotNull("Object list is null", objectList);
		assertNotNull("Light list is null", lightList);
		assertNotNull("Carema list is null", camera);

		assertTrue("Camera location wrong", camera.getLocationVector().isEqual(
				new RTVector(0.0f, 0.0f, -5.0f)));
		assertTrue("Camera sky wrong", camera.getSkyVector().isEqual(
				new RTVector(0.0f, 1.0f, 0.0f)));
		assertTrue("Camera look_at wrong", camera.getLookatVector().isEqual(
				new RTVector(0.0f, 0.0f, 0.0f)));

		assertTrue("Background value wrong", background.isEqual(new RTColor(
				0.0f, 0.0f, 1.0f)));

		assertTrue("Light position wrong", lightList.get(0).getPositionVector()
				.isEqual(new RTVector(0.0f, 0.0f, -5.0f)));

		RTSphere sphere = (RTSphere) objectList.get(0);
		assertTrue("Sphere location wrong", sphere.getLocation().isEqual(
				new RTVector(0.0, 0.0, 0.0)));
		assertEquals("Sphere radius wrong", (double) 2.0, sphere.getRadius());
		RTSurface surface = sphere.getSurface();
		assertEquals("Sphere surface diffuse wrong", 1.0f, surface.getDiffuse());
		assertEquals("Sphere surface reflect wrong", 0.0f, surface.getReflect());
		assertTrue("Sphere surface color wrong", surface.getPigment()
				.getColor().isEqual(new RTColor(1.0f, 0.0f, 0.0f)));
		assertNull(surface.getPigment().getBitmap());

		RTTriangle triangle = (RTTriangle) objectList.get(1);
		assertTrue("Triangle c0 wrong", triangle.getC0().isEqual(
				new RTVector(0.0f, 2.0f, 0.0f)));
		RTVector v1 = RTVector.sumVectors(new RTVector(4.0f, 2.0f, 0.0f),
				RTVector.reverseVector(triangle.getC0()));
		RTVector v2 = RTVector.sumVectors(new RTVector(2.0f, -2.0f, 0.0f),
				RTVector.reverseVector(triangle.getC0()));
		assertTrue("Triangle V1 wrong", triangle.getV1().isEqual(v1));
		assertTrue("Triangle V2 wrong", triangle.getV2().isEqual(v2));
		surface = triangle.getSurface();
		assertEquals("Triangle surface diffuse wrong", 1.0f, surface
				.getDiffuse());
		assertEquals("Triangle surface reflect wrong", 0.0f, surface
				.getReflect());
		assertTrue("Triangle surface color wrong", surface.getPigment()
				.getColor().isEqual(new RTColor(1.0f, 1.0f, 0.0f)));

		RTPlane plane = (RTPlane) objectList.get(2);
		assertTrue("Plan normal value wrong", plane.getNormal().isEqual(
				new RTVector(0.0f, 0.0f, 1.0f)));
		surface = plane.getSurface();
		assertEquals("Plan surface diffuse wrong", 1.0f, surface.getDiffuse());
		assertEquals("Plan surface reflect wrong", 0.0f, surface.getReflect());
		assertNull(surface.getPigment().getColor());
	}

	/**
	 * Test method for getCamera Test it works well after performing getCamera
	 */
	public final void testGetCamera() {
		RTCamera camera = importXML.getCamera();
		assertNotNull("Camera is null", camera);

		assertTrue("Camera location wrong", camera.getLocationVector().isEqual(
				new RTVector(0.0f, 0.0f, -5.0f)));
		assertTrue("Camera sky wrong", camera.getSkyVector().isEqual(
				new RTVector(0.0f, 1.0f, 0.0f)));
		assertTrue("Camera look_at wrong", camera.getLookatVector().isEqual(
				new RTVector(0.0f, 0.0f, 0.0f)));
	}

	/**
	 * Test method for getBackground Test it works well after performing
	 * getBackground
	 */
	public final void testGetBackground() {
		RTColor background = importXML.getBackground();
		assertNotNull("Background is null", background);
		assertTrue("Background value wrong", background.isEqual(new RTColor(
				0.0f, 0.0f, 1.0f)));
	}

	/**
	 * Test method for getObjectList Test it works well after performing
	 * getObjectList
	 */
	public final void testGetObjectList() {
		Vector<RTIObject> objectList = importXML.getObjectList();
		assertNotNull("Object list is null", objectList);

		RTSphere sphere = (RTSphere) objectList.get(0);
		assertTrue("Sphere location wrong", sphere.getLocation().isEqual(
				new RTVector(0.0, 0.0, 0.0)));
		assertEquals("Sphere radius wrong", (double) 2.0, sphere.getRadius());
		RTSurface surface = sphere.getSurface();
		assertEquals("Sphere surface diffuse wrong", 1.0f, surface.getDiffuse());
		assertEquals("Sphere surface reflect wrong", 0.0f, surface.getReflect());
		assertTrue("Sphere surface color wrong", surface.getPigment()
				.getColor().isEqual(new RTColor(1.0f, 0.0f, 0.0f)));
		assertNull(surface.getPigment().getBitmap());

		RTTriangle triangle = (RTTriangle) objectList.get(1);
		assertTrue("Triangle c0 wrong", triangle.getC0().isEqual(
				new RTVector(0.0f, 2.0f, 0.0f)));
		RTVector v1 = RTVector.sumVectors(new RTVector(4.0f, 2.0f, 0.0f),
				RTVector.reverseVector(triangle.getC0()));
		RTVector v2 = RTVector.sumVectors(new RTVector(2.0f, -2.0f, 0.0f),
				RTVector.reverseVector(triangle.getC0()));

		assertTrue("Triangle V1 wrong", triangle.getV1().isEqual(v1));
		assertTrue("Triangle V2 wrong", triangle.getV2().isEqual(v2));
		surface = triangle.getSurface();
		assertEquals("Triangle surface diffuse wrong", 1.0f, surface
				.getDiffuse());
		assertEquals("Triangle surface reflect wrong", 0.0f, surface
				.getReflect());
		assertTrue("Triangle surface color wrong", surface.getPigment()
				.getColor().isEqual(new RTColor(1.0f, 1.0f, 0.0f)));

		RTPlane plane = (RTPlane) objectList.get(2);
		assertTrue("Plan normal value wrong", plane.getNormal().isEqual(
				new RTVector(0.0f, 0.0f, 1.0f)));
		surface = plane.getSurface();
		assertEquals("Plan surface diffuse wrong", 1.0f, surface.getDiffuse());
		assertEquals("Plan surface reflect wrong", 0.0f, surface.getReflect());
		assertNull(surface.getPigment().getColor());

	}

	/**
	 * Test method for getLightList Test it works well after performing
	 * getLightList
	 */
	public final void testGetLightList() {
		Vector<RTLight> lightList = importXML.getLightList();
		assertNotNull("Light is null", lightList);
		assertTrue("Light position wrong", lightList.get(0).getPositionVector()
				.isEqual(new RTVector(0.0f, 0.0f, -5.0f)));
	}
}
